#!/bin/sh
# PCP QA Test No. 1874
# Exercise newer pmseries expressions and functions
# via the REST API.
#
# Copyright (c) 2020 Red Hat.  All Rights Reserved.
#

seq=`basename $0`
echo "QA output created by $seq"

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check
. ./common.keys

_check_series

_cleanup()
{
    cd $here
    [ -n "$pmproxy_pid" ] && $signal -s TERM $pmproxy_pid
    [ -n "$options" ] && $keys_cli $options shutdown
    if $need_restore
    then
	need_restore=false
	_restore_config $PCP_SYSCONF_DIR/pmseries
	_restore_config $PCP_SYSCONF_DIR/pmproxy
    fi
    $sudo rm -rf $tmp $tmp.*
}

status=0	# success is the default!
signal=$PCP_BINADM_DIR/pmsignal

userid=`id -u`
username=`id -u -n`
hostname=`hostname`
machineid=`_machine_id`
domainname=`_domain_name`

need_restore=false
trap "_cleanup; exit \$status" 0 1 2 3 15

_filter_source()
{
    sed \
	-e "s,$here,PATH,g" 
    # end
}

_filter_url()
{
    sed \
	-e "s,localhost:$proxyport/,localhost:PROXYPORT/," \
    # end
}

_curl_get()
{
    url="$1"
    echo "$url" | tee -a $seq_full | _filter_url
    curl --get --silent "$url" | pmjson | tee -a $seq_full
}

# real QA test starts here
_save_config $PCP_SYSCONF_DIR/pmproxy
_save_config $PCP_SYSCONF_DIR/pmseries
$sudo rm -f $PCP_SYSCONF_DIR/pmseries/*
$sudo rm -f $PCP_SYSCONF_DIR/pmproxy/*
need_restore=true

echo "Start test key server ..."
key_server_port=`_find_free_port`
options="-p $key_server_port"
echo "key_server_port=$key_server_port" >>$seq_full
$key_server --port $key_server_port --save "" > $tmp.keys 2>&1 &
_check_key_server_ping $key_server_port
_check_key_server $key_server_port
echo

_check_key_server_version $key_server_port

# import some well-known test data into the key server
pmseries $options --load "$here/archives/proc" | _filter_source

# start pmproxy 
proxyport=`_find_free_port`
echo "proxyport=$proxyport" >>$seq_full
proxyopts="-p $proxyport -r $key_server_port -t"  # -Dseries,http,af
pmproxy -f -U $username -x $tmp.err -Dseries,query -l $tmp.pmproxy.log $proxyopts &
pmproxy_pid=$!

# check pmproxy has started and is available for requests
pmcd_wait -h localhost@localhost:$proxyport -v -t 5sec

if [ -f $tmp.err ]
then
    echo "Arrgh: errors from key server startup ..."
    cat $tmp.err
fi

$PCP_PS_PROG $PCP_PS_ALL_FLAGS | grep -E '[P]PID|/[p]mproxy( |$)' >> $seq_full
$keys_cli $options keys pcp:* >> $seq_full
cat $tmp.pmproxy.log >> $seq_full

# Sanity check
series1=`pmseries $options disk.all.read`
[ -z "$series1" ] && _fail "Cannot find any timeseries matching disk.all.read"

#
# REST API queries using newer expression syntax and functions
#

echo "== verify series sum (a + b) SID lookup" | tee -a $seq_full
echo "kernel.all.load + kernel.all.load"
expr="kernel.all.load%20%2B%20kernel.all.load"
_curl_get "http://localhost:$proxyport/series/query?expr=$expr"

echo "== verify series sum (a + b) with samples" | tee -a $seq_full
echo "kernel.all.load[samples:1] + kernel.all.load[samples:1]"
expr="kernel.all.load%5Bsamples%3A1%5D%20%2B%20kernel.all.load%5Bsamples%3A1%5D"
_curl_get "http://localhost:$proxyport/series/query?expr=$expr"

echo "== verify series sum (a + b) with samples by SID" | tee -a $seq_full
echo "kernel.all.load + kernel.all.load"
sid="16aa36d93e5cd0733499eee7dafc07dbb2ad7067"
_curl_get "http://localhost:$proxyport/series/values?series=$sid&samples=1"
_curl_get "http://localhost:$proxyport/series/descs?series=$sid"
_curl_get "http://localhost:$proxyport/series/metrics?series=$sid"
_curl_get "http://localhost:$proxyport/series/instances?series=$sid"

# For debugging
# echo === pause for gdb -p $pmproxy_pid Log is $tmp.pmproxy.log ===
# read something

echo "== verify series difference (a - b) SID lookup" | tee -a $seq_full
echo "kernel.all.load - kernel.all.load"
expr="kernel.all.load%20-%20kernel.all.load"
_curl_get "http://localhost:$proxyport/series/query?expr=$expr"

echo "== verify series difference (a - b) with samples" | tee -a $seq_full
echo "kernel.all.load[samples:1] - kernel.all.load[samples:1]"
expr="kernel.all.load%5Bsamples%3A1%5D%20%2B%20kernel.all.load%5Bsamples%3A1%5D"
_curl_get "http://localhost:$proxyport/series/query?expr=$expr"

echo "== verify series difference (a - b) with samples by SID" | tee -a $seq_full
echo "kernel.all.load - kernel.all.load"
sid="f29c38f54d5a3cb928496439760634617688036b"
_curl_get "http://localhost:$proxyport/series/values?series=$sid&samples=1"
_curl_get "http://localhost:$proxyport/series/descs?series=$sid"
_curl_get "http://localhost:$proxyport/series/metrics?series=$sid"
_curl_get "http://localhost:$proxyport/series/instances?series=$sid"


echo "== verify series function (floor(a)) SID lookup" | tee -a $seq_full
echo "floor(kernel.all.load)"
expr="floor%28kernel.all.load%29"
_curl_get "http://localhost:$proxyport/series/query?expr=$expr"

echo "== verify series function (floor(a)) with samples" | tee -a $seq_full
echo "floor(kernel.all.load[samples:2])"
expr="floor%28kernel.all.load%5Bsamples%3A2%5D%29"
_curl_get "http://localhost:$proxyport/series/query?expr=$expr"

echo "== verify series function (floor(a)) with samples by SID" | tee -a $seq_full
echo "floor(kernel.all.load)"
sid="f2dbf7a113932b3371c8871df932603856d1d7b6"
_curl_get "http://localhost:$proxyport/series/values?series=$sid&samples=2"
_curl_get "http://localhost:$proxyport/series/descs?series=$sid"
_curl_get "http://localhost:$proxyport/series/metrics?series=$sid"
_curl_get "http://localhost:$proxyport/series/instances?series=$sid"


# success, all done
exit
